package com.sangfor.ssl.rdp.utils.crypto;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* loaded from: classes2.dex */
public class Crypto {
    static final String HEXES = "0123456789ABCDEF";
    byte[] buf;
    Cipher dcipher;
    Cipher ecipher;

    public Crypto() {
        this.buf = new byte[1024];
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            setupCrypto(keyGenerator.generateKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Crypto(String str) {
        this.buf = new byte[1024];
        setupCrypto(new SecretKeySpec(getMD5(str), "AES"));
    }

    private void setupCrypto(SecretKey secretKey) {
        IvParameterSpec ivParameterSpec = new IvParameterSpec(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
        try {
            this.ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            this.dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            this.ecipher.init(1, secretKey, ivParameterSpec);
            this.dcipher.init(2, secretKey, ivParameterSpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void encrypt(InputStream inputStream, OutputStream outputStream) {
        try {
            CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, this.ecipher);
            while (true) {
                int read = inputStream.read(this.buf);
                if (read >= 0) {
                    cipherOutputStream.write(this.buf, 0, read);
                } else {
                    cipherOutputStream.close();
                    return;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String encrypt(String str) {
        try {
            return byteToHex(this.ecipher.doFinal(str.getBytes("UTF-8")));
        } catch (Exception unused) {
            return null;
        }
    }

    public void decrypt(InputStream inputStream, OutputStream outputStream) {
        try {
            CipherInputStream cipherInputStream = new CipherInputStream(inputStream, this.dcipher);
            while (true) {
                int read = cipherInputStream.read(this.buf);
                if (read >= 0) {
                    outputStream.write(this.buf, 0, read);
                } else {
                    outputStream.close();
                    return;
                }
            }
        } catch (IOException unused) {
        }
    }

    public String decrypt(String str) {
        try {
            return new String(this.dcipher.doFinal(hexToByte(str)), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String decrypt(byte[] bArr) {
        try {
            return new String(this.dcipher.doFinal(bArr), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] getMD5(String str) {
        try {
            return MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"));
        } catch (Exception unused) {
            return null;
        }
    }

    public static String byteToHex(byte[] bArr) {
        if (bArr == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(bArr.length * 2);
        for (byte b : bArr) {
            sb.append(HEXES.charAt((b & 240) >> 4));
            sb.append(HEXES.charAt(b & 15));
        }
        return sb.toString();
    }

    public static byte[] hexToByte(String str) {
        int length = str.length();
        byte[] bArr = new byte[length / 2];
        for (int i = 0; i < length; i += 2) {
            bArr[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character.digit(str.charAt(i + 1), 16));
        }
        return bArr;
    }
}
